/*
 * << Haru Free PDF Library >> -- hpdf_ext_text.c
 *
 * URL: http://libharu.org
 *
 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
 * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.
 * It is provided "as is" without express or implied warranty.
 *
 */

#include "hpdf_conf.h"
#include "hpdf_utils.h"
#include "hpdf_pages.h"
#include "hpdf.h"
#include "dataproc.h"
#include "../ext/hpdf_ext.h"

/* Td */
HPDF_EXPORT(HPDF_STATUS)
hpdf_page_move_text_pos(
	HPDF_Page page,
	HPDF_INT32 x,
	HPDF_INT32 y)
{
	HPDF_STATUS ret = HPDF_Page_CheckState(page, HPDF_GMODE_TEXT_OBJECT);
	char pdf_buf[HPDF_TMP_BUF_SIZ];
	char length = 0;
	HPDF_PageAttr attr;

	HPDF_PTRACE((" HPDF_Page_MoveTextPos\n"));

	if (ret != HPDF_OK)
		return ret;

	attr = (HPDF_PageAttr)page->attr;

	length = i32_to_string(pdf_buf, x, 0x80, 0x03);
	pdf_buf[length++] = ' ';
	length += i32_to_string(&pdf_buf[length], y, 0x80, 0x03);

	length += copy_str(" Td\n", &pdf_buf[length], 10);

	if (HPDF_Stream_WriteStr(attr->stream, pdf_buf) != HPDF_OK)
		return HPDF_CheckError(page->error);
	/*
		attr->text_matrix.x += x * attr->text_matrix.a + y * attr->text_matrix.c;
		attr->text_matrix.y += y * attr->text_matrix.d + x * attr->text_matrix.b;
		attr->text_pos.x = attr->text_matrix.x;
		attr->text_pos.y = attr->text_matrix.y;
	*/
	return ret;
}

HPDF_EXPORT(HPDF_STATUS)
hpdf_page_p1_move_text_pos(
	HPDF_Page page,
	HPDF_INT32 x,
	HPDF_INT32 y)
{
	HPDF_STATUS ret = HPDF_Page_CheckState(page, HPDF_GMODE_TEXT_OBJECT);
	char pdf_buf[HPDF_TMP_BUF_SIZ];
	char length = 0;
	HPDF_PageAttr attr;

	HPDF_PTRACE((" HPDF_Page_MoveTextPos\n"));

	if (ret != HPDF_OK)
		return ret;

	attr = (HPDF_PageAttr)page->attr;

	length = i32_to_string(pdf_buf, x, 0x61, 0x03);
	pdf_buf[length++] = ' ';
	length += i32_to_string(&pdf_buf[length], y, 0x81, 0x03);

	length += copy_str(" Td\n", &pdf_buf[length], 10);

	if (HPDF_Stream_WriteStr(attr->stream, pdf_buf) != HPDF_OK)
		return HPDF_CheckError(page->error);
	/*
		attr->text_matrix.x += x * attr->text_matrix.a + y * attr->text_matrix.c;
		attr->text_matrix.y += y * attr->text_matrix.d + x * attr->text_matrix.b;
		attr->text_pos.x = attr->text_matrix.x;
		attr->text_pos.y = attr->text_matrix.y;
	*/
	return ret;
}

/* Tj */
HPDF_EXPORT(HPDF_STATUS)
hpdf_page_show_text(
	HPDF_Page page,
	const char *text)
{

	HPDF_STATUS ret = HPDF_Page_CheckState(page, HPDF_GMODE_TEXT_OBJECT);
	char pdf_buf[HPDF_TMP_BUF_SIZ];
	char length = 0;
	HPDF_PageAttr attr;

	ii8 isCNSInside = EXT_FALSE;
	uu8 *pStr = (uu8 *)text;

	HPDF_PTRACE((" HPDF_Page_ShowText\n"));

	if (ret != HPDF_OK || text == NULL || text[0] == 0)
		return ret;

	attr = (HPDF_PageAttr)page->attr;

	/* 检测显示字符中是否包含中文,即多字节编码的数据 */
	while (*pStr)
	{
		uint8_t ch = *pStr;

		if (ch >= 0x80 || ch == '(' || ch == ')' || ch == '\\')
		{
			isCNSInside = EXT_TRUE;
			break;
		}
		pStr++;
	}

	if (isCNSInside)
	{
		/* 中文 字符串输出*/
		ret = hpdf_page_show_text_gbk(page, text);
		length = 0; // 已加过
	}
	else
	{
		/* ascii 字符串输出 */
		if (HPDF_Stream_WriteStr(attr->stream, "(") != HPDF_OK)
			return HPDF_CheckError(page->error);
		if (HPDF_Stream_WriteStr(attr->stream, text) != HPDF_OK)
			return HPDF_CheckError(page->error);
		if (HPDF_Stream_WriteStr(attr->stream, ") Tj\012") != HPDF_OK)
			return HPDF_CheckError(page->error);
	}

	return ret;
}

HPDF_EXPORT(HPDF_STATUS)
hpdf_page_show_text_check(HPDF_Page page)
{
	return hpdf_page_show_text(page, 0);
}

/*****************************************************************************
 * @brief   spdf_page_end_text：文本写入
 * @param   page       ：句柄
 * @param   text       ：文本内容
 * @return  执行状态
 *****************************************************************************/
HPDF_EXPORT(HPDF_STATUS)
hpdf_page_show_text_ascii(HPDF_Page page, const char *text)
{
	HPDF_PageAttr attr;

	attr = (HPDF_PageAttr)page->attr;

	/* ascii 字符串输出 */
	if (HPDF_Stream_WriteStr(attr->stream, "(") != HPDF_OK)
		return HPDF_CheckError(page->error);
	if (HPDF_Stream_WriteStr(attr->stream, text) != HPDF_OK)
		return HPDF_CheckError(page->error);
	if (HPDF_Stream_WriteStr(attr->stream, ") Tj\012") != HPDF_OK)
		return HPDF_CheckError(page->error);

	return HPDF_OK;
}

/*****************************************************************************
 * @brief   spdf_page_end_text：文本写入
 * @param   page       ：句柄
 * @param   text       ：文本内容
 * @return  执行状态
 *****************************************************************************/
HPDF_EXPORT(HPDF_STATUS)
hpdf_page_show_text_gbk(HPDF_Page page, const char *text)
{
	char pdf_buf[HPDF_TMP_BUF_SIZ];
	char length = 0;
	HPDF_PageAttr attr;
	HPDF_STATUS ret;
	uu8 ch, lo, ho;

	attr = (HPDF_PageAttr)page->attr;

	pdf_buf[length++] = '<';

	while (*text)
	{
		ch = *text++;
		lo = ch & 0x0F;
		ho = ch >> 4;

		if (ho < 10)
		{
			pdf_buf[length++] = ho + '0';
		}
		else
		{
			pdf_buf[length++] = ho + 'A' - 10;
		}

		if (lo < 10)
		{
			pdf_buf[length++] = lo + '0';
		}
		else
		{
			pdf_buf[length++] = lo + 'A' - 10;
		}
	}
	pdf_buf[length] = 0;

	length += copy_str("> Tj\n", &pdf_buf[length], 10);

	if (HPDF_Stream_WriteStr(attr->stream, pdf_buf) != HPDF_OK)
		return HPDF_CheckError(page->error);

	return HPDF_OK;
}

// 设置填充颜色-用于文本
HPDF_EXPORT(HPDF_STATUS)
hpdf_page_set_text_rgb(
	HPDF_Page page,
	HPDF_INT8 r,
	HPDF_INT8 g,
	HPDF_INT8 b)
{
	HPDF_STATUS ret = HPDF_Page_CheckState(page, HPDF_GMODE_TEXT_OBJECT |
													 HPDF_GMODE_PAGE_DESCRIPTION);
	char pdf_buf[HPDF_TMP_BUF_SIZ];
	char length = 0;
	HPDF_PageAttr attr;

	HPDF_PTRACE((" HPDF_Page_SetRGBFill\n"));

	if (ret != HPDF_OK)
		return ret;

	if (r < 0 || r > 100 || g < 0 || g > 100 || b < 0 || b > 100)
		return HPDF_RaiseError(page->error, HPDF_PAGE_OUT_OF_RANGE, 0);

	attr = (HPDF_PageAttr)page->attr;

	length += i32_to_string(&pdf_buf[length], r, 0x42, 0x03);
	pdf_buf[length++] = ' ';

	length += i32_to_string(&pdf_buf[length], g, 0x42, 0x03);
	pdf_buf[length++] = ' ';

	length += i32_to_string(&pdf_buf[length], b, 0x42, 0x03);

	copy_str(" rg\n", &pdf_buf[length], 10);

	if (HPDF_Stream_WriteStr(attr->stream, pdf_buf) != HPDF_OK)
		return HPDF_CheckError(page->error);

	// attr->gstate->rgb_stroke.r = r;
	// attr->gstate->rgb_stroke.g = g;
	// attr->gstate->rgb_stroke.b = b;
	attr->gstate->cs_stroke = HPDF_CS_DEVICE_RGB;

	return ret;
}

/*****************************************************************************
 * @brief   spdf_page_set_font：设置字体
 * @param   page       ：句柄
 * @param   font_name  ：字体名
 * @param   size       ：
 * @return  执行状态
 *****************************************************************************/
HPDF_EXPORT(HPDF_STATUS)
hpdf_page_set_font(
	HPDF_Page page,
	HPDF_Font font, uu8 size)
{
	return HPDF_Page_SetFontAndSize(page, font, size);
}
